﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Autofac;
using MgSoft.K3Cloud.WebApi.Dto;
using Newtonsoft.Json;

namespace MgSoft.K3Cloud.WebApi
{
    /// <summary>
    /// 金蝶API定义
    /// </summary>
    public class K3CloudApiExecuter
    {
        public ILifetimeScope lifetimeScope { get; set; }
        public KingdeeHttpClientHelper kingdeeHttpClient => lifetimeScope.Resolve<KingdeeHttpClientHelper>();
        public ApiServerInfo ApiServerInfo => lifetimeScope?.Resolve<ApiServerInfo>();
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="k3CloudClientInfo"></param>
        /// <returns></returns>
        public string ValidateUser(K3CloudClientInfo k3CloudClientInfo) => ValidateUser(k3CloudClientInfo, CancellationToken.None);
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="k3CloudClientInfo"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public string ValidateUser(K3CloudClientInfo k3CloudClientInfo, CancellationToken cancellationToken)
        {
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");

            var httpContent = GetContents(k3CloudClientInfo.Dbid, k3CloudClientInfo.UserName, k3CloudClientInfo.Password, k3CloudClientInfo.Lcid);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="k3CloudClientInfo"></param>
        /// <returns></returns>
        public string Logout(K3CloudClientInfo k3CloudClientInfo) => Logout(k3CloudClientInfo, CancellationToken.None);
        public string Logout(K3CloudClientInfo k3CloudClientInfo, CancellationToken cancellationToken)
        {
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.Logout.common.kdsvc");

            var httpContent = GetContents(k3CloudClientInfo.Dbid, k3CloudClientInfo.UserName, k3CloudClientInfo.Password, k3CloudClientInfo.Lcid);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }


        /// <summary>
        /// 暂存
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string Draft(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Draft.common.kdsvc");

            var httpContent = GetContents(formId, content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string Save(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");

            var httpContent = GetContents(formId, content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }
        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string BatchSave(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc");

            var httpContent = GetContents(formId, content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string Submit(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
            var httpContent = GetContents(formId, content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        public string ExecuteBillQuery(string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc");

            var httpContent = GetContents(content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string Delete(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc");

            var httpContent = GetContents(formId, content);

            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string Audit(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");

            var httpContent = GetContents(formId, content);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }
        /// <summary>
        /// 反审核
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string UnAudit(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc");

            var httpContent = GetContents(formId, content);

            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }
        /// <summary>
        /// 下推
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求Json串</param>
        /// <returns></returns>
        public string Push(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc");

            var httpContent = GetContents(formId, content);

            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 分配
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求Json串</param>
        /// <returns></returns>
        public string Allocate(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Allocate.common.kdsvc");

            var httpContent = GetContents(formId, content);

            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="content">请求JSON串</param>
        /// <returns></returns>
        public string View(string formId, string content, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc");

            var httpContent = GetContents(formId, content);

            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        /// <summary>
        /// 自定义
        /// </summary>
        /// <param name="key">自定义方法标识</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public string AbstractWebApiBusinessService(string key, List<object> args, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, key, ".common.kdsvc");

            var httpContent = JsonConvert.SerializeObject(args);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }
        /// <summary>
        /// 自定义
        /// </summary>
        /// <param name="key">自定义方法标识</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public string AbstractWebApiBusinessService(string key, object args, CancellationToken cancellationToken)
        {
            K3CloudClientInfo k3CloudClientInfo = new K3CloudClientInfo(ApiServerInfo);
            var httpUrl = string.Concat(k3CloudClientInfo.ServerUrl, key, ".common.kdsvc");

            var httpContent = JsonConvert.SerializeObject(args);
            return kingdeeHttpClient.SysncRequest(httpUrl, httpContent, cancellationToken);
        }

        #region 私有方法
        private string GetContents(params Object[] objs)
        {
            List<object> Parameters = new List<object>();
            foreach (var obj in objs)
            {
                Parameters.Add(obj);
            }
            return JsonConvert.SerializeObject(Parameters);
        }
        #endregion
    }
}
